package com.ruoyi.web.controller.second;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.PlusGradeItem;
import com.ruoyi.system.service.IPlusGradeItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 加分项Controller
 *
 * @author 王清江
 * @date 2021-01-31
 */
@RestController
@RequestMapping("/system/item")
public class PlusGradeItemController extends BaseController {
    @Autowired
    private IPlusGradeItemService plusGradeItemService;

    /**
     * 查询加分项列表
     */
    @PreAuthorize("@ss.hasPermi('student:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlusGradeItem plusGradeItem) {
        startPage();
        List<PlusGradeItem> list = plusGradeItemService.selectPlusGradeItemList(plusGradeItem);
        return getDataTable(list);
    }

    /**
     * 导出加分项列表
     */
    @PreAuthorize("@ss.hasPermi('student:item:export')")
    @Log(title = "加分项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PlusGradeItem plusGradeItem) {
        List<PlusGradeItem> list = plusGradeItemService.selectPlusGradeItemList(plusGradeItem);
        ExcelUtil<PlusGradeItem> util = new ExcelUtil<PlusGradeItem>(PlusGradeItem.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 获取加分项详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:item:query')")
    @GetMapping(value = "/{plusGradeItemId}")
    public AjaxResult getInfo(@PathVariable("plusGradeItemId") Long plusGradeItemId) {
        return AjaxResult.success(plusGradeItemService.selectPlusGradeItemById(plusGradeItemId));
    }

    /**
     * 新增加分项
     */
    @PreAuthorize("@ss.hasPermi('student:item:add')")
    @Log(title = "加分项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlusGradeItem plusGradeItem) {
        return toAjax(plusGradeItemService.insertPlusGradeItem(plusGradeItem));
    }

    /**
     * 修改加分项
     */
    @PreAuthorize("@ss.hasPermi('student:item:edit')")
    @Log(title = "加分项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlusGradeItem plusGradeItem) {
        return toAjax(plusGradeItemService.updatePlusGradeItem(plusGradeItem));
    }

    /**
     * 删除加分项
     */
    @PreAuthorize("@ss.hasPermi('student:item:remove')")
    @Log(title = "加分项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{plusGradeItemIds}")
    public AjaxResult remove(@PathVariable Long[] plusGradeItemIds) {
        return toAjax(plusGradeItemService.deletePlusGradeItemByIds(plusGradeItemIds));
    }

    /**
     * 学院状态修改，需要权限college/approval/status方可进行操作，需要管理员到前端的教师入口手动赋权
     */
    @PreAuthorize("@ss.hasPermi('college/approval/status')")
    @Log(title = "加分项", businessType = BusinessType.UPDATE)
    @PutMapping("/changeCollegeStatus")
    public AjaxResult changeCollegeStatus(@RequestBody PlusGradeItem plusGradeItem) {
        return toAjax(plusGradeItemService.updatePlusGradeItemOnlyStatus(plusGradeItem));
    }

    /**
     * 学生处状态修改，需要权限teacher/office/list方可进行操作，需要管理员到前端的教师入口手动赋权
     * 这个接口和上面的接口其实本质完全是一样的，但是接受的参数是statusOffice，拥有了teacher/office/list权限的人
     * 才能够请求这个接口，当拥有了这个权限之后，也可以在控制台进行代码的修改---不是漏洞
     */
    @PreAuthorize("@ss.hasPermi('teacher/office/list')")
    @Log(title = "加分项", businessType = BusinessType.UPDATE)
    @PutMapping("/changeOfficeStatus")
    public AjaxResult changeOfficeStatus(@RequestBody PlusGradeItem plusGradeItem) {
        return toAjax(plusGradeItemService.updatePlusGradeItemOnlyStatus(plusGradeItem));
    }


    /**
     * 学生处状态修改，需要权限student:item:calc方可进行操作，需要管理员到前端的教师入口手动赋权
     * 这个接口和上面的接口其实本质完全是一样的，但是不接受任何参数，拥有了student:item:calc权限的人
     * 才能够请求这个接口，当拥有了这个权限之后，也可以在控制台进行代码的修改---不是漏洞
     */
    @PreAuthorize("@ss.hasPermi('student:item:calc')")
    @Log(title = "异步算分", businessType = BusinessType.UPDATE)
    @GetMapping("/calculate")
    public AjaxResult calculate(PlusGradeItem plusGradeItem)
            throws ExecutionException, InterruptedException, TimeoutException {
        //仅仅触发算法机制，并且是异步，放进线程池中进行计算
        if (plusGradeItem != null && plusGradeItem.getUserId() != null) {
            return AjaxResult.success(AsyncManager
                    .me()
                    .submit(AsyncFactory
                            .calculate(plusGradeItem
                                    .getUserId()))
                    .get(3L, TimeUnit.SECONDS) + ""
            );
        }
        return AjaxResult.success(AsyncManager
                .me()
                .submit(AsyncFactory
                        .calculate(SecurityUtils
                                .getLoginUser()
                                .getUser()
                                .getUserId()))
                .get(3L, TimeUnit.SECONDS) + ""
        );
    }


}
